package hc.bre.policy

import hc.bre.cmn.IListOption
import hc.bre.cmn.ListOption
import hc.bre.cmn.RuleScriptBuilder

@groovy.transform.CompileStatic
class ReasonBuilder extends RuleScriptBuilder<ReasonDefinition> {
	@Override
	public void buildClassMemberClass() {
		apdInnerClass("ReasonItem",null, IListOption.class){
			apdNlBlock(IListOption.declarationOfOption());
		}
	}

	@Override
	public void buildClassMemberVariable() {
		List<ReasonItem> lst = ruleDef.list;

		lst.each { ReasonItem it ->
			apdStaticFinalVar("ReasonItem"
					,it.code
					,"new ReasonItem(id:${it.id}, code:${referText(it.code)}, name:${referText(it.name)})");
		}
	}
	
	@Override
	public void buildClassRuleBodyCore() {
		String commonOption = className(ListOption.class);
		
		apdNl("List<ReasonItem> _policyset_ = [];")

		ruleDef.list.each {
			apdNl("if((_reasoncode_ == '_all_' || _reasoncode_ =='${it.code}') && {");
			apdNl("\t ${it.script}").apdNl("\t}.call())").apd(" _policyset_.add(${referRuleClass()}.${it.code});")
		}

		apdNl("${result}.policyset = _policyset_;")
	}
}
